from turtle import *

# 陀螺状态字典
state = {'turn': 0}


# 画陀螺
def spinner():
    clear()
    angle = state['turn'] / 10
    right(angle)
    forward(100)
    dot(120, 'red')
    back(100)
    right(120)
    forward(100)
    dot(120, 'green')
    back(100)
    right(120)
    forward(100)
    dot(120, 'blue')
    back(100)
    right(120)
    update()


# 陀螺动画
def animate():
    if state['turn'] > 0:
        state['turn'] -= 1
    spinner()
    ontimer(animate, 20)


# 点击陀螺，改变速度
def flick():
    state['turn'] += 10


ht()
tracer(False)
pensize(20)
onkey(flick, 'space')
listen()
animate()
done()
